#!/user/bin/env python
# -*- coding:utf-8 -*-
#filename:controllers/numsjson.py

'''

获取uid获取一些统计数据：我的tfeet数量，我评论的数量，我的tfeet被评论的数量，我收藏tfeet数量。

http://tinyfeet.us/i/nums/1231?srckey=234234saeasdfasdfasdf

Created on 2011-8-30

@author: zc

modify on 2011-09-12  None json--> {}

'''

from dbhandle import mysqldb
from config import sqlmap


class numsjson():
    '''    classdocs    '''
    __dbo = mysqldb.mysqldb()
    def numsjson( self, uid = 0 ):
        if uid <> 0:
            """ mytfeet """
            sql_mytfeet = sqlmap.SQL_MYFEETS % ( uid )
            
            """ mycomments """
            sql_mycomments = sqlmap.SQL_MYCMTS % ( uid )
            
            """ theircomments """
            sql_theircomments = sqlmap.SQL_THCMTS % ( uid )
            
            """ my fav"""
            sql_myfav = sqlmap.SQL_MYFAVS % ( uid )
            
            mytf = self.execsql( sql_mytfeet )
            if not mytf: mytf = 0
            
            mycm = self.execsql( sql_mycomments )
            if not mycm: mycm = 0
            
            myth = self.execsql( sql_theircomments )
            if not myth: myth = 0
            
            myfv = self.execsql( sql_myfav )
            if not myfv: myfv = 0

            nums = ""
            nums = nums + " { "
            nums = nums + '''"tfeets":"''' + str( mytf ) + '''",'''
            nums = nums + '''"mycomments":"''' + str( mycm ) + '''",'''
            nums = nums + '''"theircomments":"''' + str( myth ) + '''",'''
            nums = nums + '''"myfavorite":"''' + str( myfv ) + '''"'''
            nums = nums + "}"
                
            return nums
      

            

    def execsql( self, sql ):
        res = self.__dbo.query( sql )
        rescnt = 0
        if res:
            rescnt = res[0].cnt
            #for r in res:
            #    rescnt = r.cnt
            
        return rescnt
    

if __name__ == "__main__":
    ts = numsjson()
    print ts.numsjson( 1 )

